package com.sxit.tdf.server.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxit.tdf.server.entity.Classes;
import com.sxit.tdf.server.entity.bo.DetailClassInfoBo;
import com.sxit.tdf.server.entity.bo.SimpleClassesBo;
import com.sxit.tdf.server.entity.vo.DeleteClasses;
import com.sxit.tdf.server.entity.vo.InsertClassesVo;
import com.sxit.tdf.server.entity.vo.SiftClassesInfoVo;
import com.sxit.tdf.server.exception.TdfException;
import com.sxit.tdf.server.response.R;
import com.sxit.tdf.server.service.IClassesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;

/**
 * 前端控制器
 *
 * @author 橙子高
 * @since 2022-02-13
 */
@RestController
@RequestMapping("/classes")
@Tag(name = "ClassesController", description = "课程信息相关接口")
public class ClassesController {

  @Autowired private IClassesService classesService;

  @Operation(summary = "获取简易的班级信息")
  @GetMapping("/getClassesList")
  @SaCheckLogin
  public R getClassesList() {

    return R.ok().put("data", classesService.getSimpleClassesInfoBo());
  }

  @Operation(summary = "获取课程信息列表")
  @PostMapping("/getClassesList")
  @SaCheckLogin
  @SaCheckPermission("CLASSES:SELECT")
  public R getClassesList(@RequestBody SiftClassesInfoVo siftClassesInfoVo) {

    IPage<SimpleClassesBo> classList = classesService.getClassList(siftClassesInfoVo);

    return R.ok().put("data", classList.getRecords()).put("total", classList.getTotal());
  }

  @Operation(summary = "获取最早的年级")
  @GetMapping("/getEarlyYear")
  @SaCheckLogin
  public R getEarlyYear() {
    int earlyYear = classesService.getEarlyYear();
    return R.ok().put("data", earlyYear);
  }

  @Operation(summary = "插入新的班级数据")
  @PostMapping("/insertOrUpdateClasses")
  @SaCheckPermission(
      value = {"CLASSES:UPDATE", "CLASSES:INSERT"},
      mode = SaMode.OR)
  public R insertClasses(@Valid @RequestBody InsertClassesVo insertClassesVo) {

    Classes classes = new Classes();

    if (insertClassesVo.getId() == null) {
      classes.setClassStatu(insertClassesVo.getClassStatu());
      classes.setCreateTime(LocalDateTime.now());
      classes.setUpdateTime(LocalDateTime.now());

      classes.setClassName(insertClassesVo.getClassName());
      classes.setClassYear(insertClassesVo.getClassYear());
      classes.setDeptId(insertClassesVo.getDeptName());
      classes.setProfessionalId(insertClassesVo.getProfessionalName());
      classes.setUserList("[]");

      boolean save = classesService.save(classes);
      if (save) {
        return R.ok("保存成功");
      } else {
        return R.error("保存失败");
      }
    } else {
      classes.setClassStatu(insertClassesVo.getClassStatu());

      classes.setUpdateTime(LocalDateTime.now());
      classes.setId(insertClassesVo.getId());
      classes.setClassName(insertClassesVo.getClassName());
      classes.setClassYear(insertClassesVo.getClassYear());
      classes.setDeptId(insertClassesVo.getDeptName());
      classes.setProfessionalId(insertClassesVo.getProfessionalName());

      boolean save = classesService.updateById(classes);
      if (save) {
        return R.ok("修改成功");
      } else {
        return R.error("修改失败");
      }
    }
  }

  @Operation(summary = "通过Id字段删除班级")
  @PostMapping("/deleteClaasesById")
  @SaCheckLogin
  @SaCheckPermission("CLASSES:DELETE")
  public R deleteClaasesById(@Valid @RequestBody DeleteClasses deleteClasses) {

    boolean delete = classesService.deleteById(deleteClasses.getId());

    if (delete) {
      return R.ok("删除成功");
    } else {
      throw new TdfException("删除失败");
    }
  }

  @Operation(summary = "通过Id获取课程基本信息")
  @GetMapping("/getClassesById")
  @SaCheckLogin
  @SaCheckPermission("CLASSES:SELECT")
  public R getClassesById(@PathParam("id") int id) {
    Classes byId = classesService.getById(id);
    return R.ok().put("data", byId);
  }

  @Operation(summary = "通过Id获取课程的详细信息")
  @GetMapping("/getDetailedClassesIngo")
  @SaCheckLogin
  @SaCheckPermission("CLASSES:SELECT")
  public R getDetailedClassesIngo(@PathParam("id") int id) {
    DetailClassInfoBo detailClassInfoBo = classesService.getDetailClassInfoBo(id);
    return R.ok().put("data", detailClassInfoBo);
  }
}
